﻿#if !LITE
using System;
using System.Collections.Generic;
using System.Text;

using Elegant.Ui;

namespace iPdf
{
    class PdfToXPage : ConvertPage<PdfToXOption>
    {
        #region Properties

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.PdfToXPageIntroInfo;
            }
        }

        public override FileFormat DestinationFormat
        {
            get
            {
                FileFormat format = new FileFormat();
                format.AddExtension(PdfCreatorOption.FormatToExtension(this.FormatGallery.SelectedFormat));
                return format;
            }
        }

        protected override PdfToXOption PageOption
        {
            get
            {
                PdfToXOption option = new PdfToXOption();
                option.Format = this.FormatGallery.SelectedFormat;
                option.XResolution = (int)this.XResolution.Value;
                option.YResolution = (int)this.YResolution.Value;
                return option;
            }
        }

        public override bool SessionEnabled
        {
            get
            {
                return true;
            }
        }

        private bool IsPdfFormat
        {
            get
            {
                return this.FormatGallery.SelectedFormat == PdfCreatorFormat.PDF ||
                        this.FormatGallery.SelectedFormat == PdfCreatorFormat.PDF_A ||
                        this.FormatGallery.SelectedFormat == PdfCreatorFormat.PDF_X;
            }
        }

        public override string ActionName
        {
            get
            {
                return base.ActionName.Replace("X", 
                    this.FormatGallery.SelectedFormat.ToString() + "_by" +
                    this.XResolution.Value.ToString() + "X" + this.YResolution.Value.ToString() );
            }
        }

        #endregion

        #region Ctor
        public PdfToXPage()
        {
            visibleColumns.Add(Global.Grid.From);
            visibleColumns.Add(Global.Grid.To);

            actor = new PdfToXConverter();
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();

            this.XResolution = new Elegant.Ui.NumericUpDown();
            this.YResolution = new Elegant.Ui.NumericUpDown();
            this.XResolutionUnit = new Elegant.Ui.Label();
            this.YResolutionUnit = new Elegant.Ui.Label();

            this.FormatGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.XResolution)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.YResolution)).BeginInit();
            this.SuspendLayout();

            this.FormatGroup.Controls.Add(this.XResolution);
            this.FormatGroup.Controls.Add(this.YResolution);
            this.FormatGroup.Controls.Add(this.XResolutionUnit);
            this.FormatGroup.Controls.Add(this.YResolutionUnit);

            // 
            // XResolution
            // 
            this.XResolution.Id = "c39a3624-67b4-44f8-8fa0-46227c009eda";
            this.XResolution.LabelText = "X Resolution";
            this.XResolution.Location = new System.Drawing.Point(4, 10);
            this.XResolution.Maximum = new decimal(new int[] {
            MaxPdfResolution,
            0,
            0,
            0});
            this.XResolution.Minimum = new decimal(new int[] {
            MinPdfResolution,
            0,
            0,
            0});
            this.XResolution.Name = "XResolution";
            this.XResolution.Size = new System.Drawing.Size(119, 24);
            this.XResolution.TabIndex = 0;
            this.XResolution.TextEditorWidth = 27;
            this.XResolution.Value = new decimal(new int[] {
            PdfXResolution,
            0,
            0,
            0});
            this.FormatGroup.SetVerticallyDistributed(this.XResolution, true);
            // 
            // YResolution
            // 
            this.FormatGroup.SetFlowBreak(this.YResolution, true);
            this.YResolution.Id = "9d922af4-9cb5-45aa-849d-f4102fab2dba";
            this.YResolution.LabelText = "Y Resolution";
            this.YResolution.Location = new System.Drawing.Point(4, 42);
            this.YResolution.Maximum = new decimal(new int[] {
            MaxPdfResolution,
            0,
            0,
            0});
            this.YResolution.Minimum = new decimal(new int[] {
            MinPdfResolution,
            0,
            0,
            0});
            this.YResolution.Name = "YResolution";
            this.YResolution.Size = new System.Drawing.Size(119, 24);
            this.YResolution.TabIndex = 1;
            this.YResolution.TextEditorWidth = 27;
            this.YResolution.Value = new decimal(new int[] {
            PdfYResolution,
            0,
            0,
            0});
            this.FormatGroup.SetVerticallyDistributed(this.YResolution, true);
            // 
            // XResolutionUnit
            // 
            this.XResolutionUnit.Id = "985a434f-59f1-4c4e-a5d6-14f6f6c0632e";
            this.XResolutionUnit.Location = new System.Drawing.Point(124, 10);
            this.XResolutionUnit.Name = "XResolutionUnit";
            this.XResolutionUnit.Size = new System.Drawing.Size(17, 24);
            this.XResolutionUnit.TabIndex = 2;
            this.XResolutionUnit.Text = "dpi";
            this.XResolutionUnit.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
            this.FormatGroup.SetVerticallyDistributed(this.XResolutionUnit, true);
            // 
            // YResolutionUnit
            // 
            this.YResolutionUnit.Id = "946c519a-5090-4420-8867-96038e2397bd";
            this.YResolutionUnit.Location = new System.Drawing.Point(124, 42);
            this.YResolutionUnit.Name = "YResolutionUnit";
            this.YResolutionUnit.Size = new System.Drawing.Size(17, 24);
            this.YResolutionUnit.TabIndex = 3;
            this.YResolutionUnit.Text = "dpi";
            this.YResolutionUnit.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
            this.FormatGroup.SetVerticallyDistributed(this.YResolutionUnit, true);

            this.FormatGroup.ResumeLayout(false);
            this.FormatGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.XResolution)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.YResolution)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        #region Methods

        protected override void InstallEventHandlers()
        {
            this.FormatGallery.SelectedItemChanged += new EventHandler<GallerySelectedItemChangedEventArgs>(FormatGallery_SelectedItemChanged);

            this.XResolution.ValueChanged += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    int value = (int)XResolution.Value;
                    if (IsPdfFormat)
                    {
                        PdfXResolution = value;
                    }
                    else
                    {
                        ImageXResolution = value;
                    }
                });

            this.YResolution.ValueChanged += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    int value = (int)YResolution.Value;
                    if (IsPdfFormat)
                    {
                        PdfYResolution = value;
                    }
                    else
                    {
                        ImageYResolution = value;
                    }
                });
        }

        private void FormatGallery_SelectedItemChanged(object sender, GallerySelectedItemChangedEventArgs e)
        {
            if (IsPdfFormat)
            {
                UpdateResolution(PdfXResolution, PdfYResolution, MaxPdfResolution, MinPdfResolution);
            }
            else
            {
                UpdateResolution(ImageXResolution, ImageYResolution, MaxImageResolution,
                    MinImageResolution);
            }

            Global.Grid.UpdateOutputExtension();
        }

        private void UpdateResolution(int xRes, int yRes, int maxRes, int minRes)
        {
            this.XResolution.Maximum = (decimal)maxRes;
            this.XResolution.Minimum = (decimal)minRes;
            this.XResolution.Value = (decimal)xRes;

            this.YResolution.Maximum = (decimal)maxRes;
            this.YResolution.Minimum = (decimal)minRes;
            this.YResolution.Value = (decimal)yRes;
        }

        public override string GetDestination(string item, string outputFolder)
        {
            string destination = base.GetDestination(item, outputFolder);

            if (IsPdfFormat)
            {
                int lastDotIndex = destination.LastIndexOf(Helper.Dot);
                destination = destination.Insert(lastDotIndex, Helper.Dot + this.FormatGallery.SelectedFormat.ToString());
            }

            return destination;
        }

        public override void ImportSession(System.Xml.XmlElement sessionNode)
        {
            base.ImportSession(sessionNode);

            this.FormatGallery.SelectedFormat = (PdfCreatorFormat) Enum.Parse(
                typeof(  PdfCreatorFormat ), sessionNode.GetAttribute(formatAttr), true);
            this.XResolution.Value = (decimal)Int32.Parse(sessionNode.GetAttribute(xResolutionAttr));
            this.YResolution.Value = (decimal)Int32.Parse(sessionNode.GetAttribute(yResolutionAttr));
        }

        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            base.ExportSession(sessionNode);

            SetXmlElementAttribute(sessionNode, formatAttr, this.FormatGallery.SelectedFormat.ToString());
            SetXmlElementAttribute(sessionNode, xResolutionAttr, this.XResolution.Value.ToString());
            SetXmlElementAttribute(sessionNode, yResolutionAttr, this.YResolution.Value.ToString());
        }

        #endregion

        #region Fields
        private Elegant.Ui.NumericUpDown XResolution;
        private Elegant.Ui.NumericUpDown YResolution;
        private Elegant.Ui.Label XResolutionUnit;
        private Elegant.Ui.Label YResolutionUnit;

        private int PdfXResolution = 600;
        private int PdfYResolution = 600;
        private const int MinPdfResolution = 100;
        private const int MaxPdfResolution = 4000;

        private int ImageXResolution = 200;
        private int ImageYResolution = 200;
        private const int MinImageResolution = 100;
        private const int MaxImageResolution = 600;

        private const string bookmarkAttr = "Bookmark";
        private const string formatAttr = "Format";
        private const string xResolutionAttr = "XResolution";
        private const string yResolutionAttr = "YResolution";
        #endregion
    }
}

#endif